import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/refresh/base_refresh.dart';
import 'package:flutter_09_game/base/widget/base_list_more_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/game/game_detail/models/model_game_round.dart';
import 'package:flutter_09_game/pages/game/game_detail/widgets/widget_round_ball.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'game_mine_results_logic.dart';

class GameMineResultsPage extends StatelessWidget {
  GameMineResultsPage({super.key});

  final GameMineResultsLogic logic = Get.put(GameMineResultsLogic());

  @override
  Widget build(BuildContext context) {
    return BaseListMoreGetBuilder<GameMineResultsLogic>(
      appBar: CommonAppBar(title: LocaleKeys.betAmount.tr),
      bodyTopViewBuilder: (_) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          height: 40.w,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildHeaderCell(
                LocaleKeys.drawBlock.tr,
              ),
              _buildHeaderCell(
                LocaleKeys.currency.tr,
              ),
              _buildHeaderCell(LocaleKeys.stakeAmount.tr),
              _buildHeaderCell(
                LocaleKeys.historyWinLose.tr,
              ),
            ],
          ),
        );
      },
      builder: (_) {
        return ListView.builder(
            itemCount: logic.list.length,
            itemBuilder: (c, index) {
              return _buildTableRow(logic.list[index]);
            });
      },
    );
  }

  /// 构建数据行
  Widget _buildTableRow(ModelGameRoundMyHistory item, {bool isFirst = false}) {
    return Container(
      height: 40.w,
      margin: EdgeInsets.symmetric(vertical: 2.w),
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: isFirst ? AppColor.primary.withOpacity(0.3) : null,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildDataCell(
            _formatRound(item.round),
          ),
          _buildCurrencyCell(
            item.symbol ?? 'USDT',
          ),
          _buildDataCell(
            item.amount.formatWallet(),
          ),
          _buildProfitLossCell(
            item.isWin == true ? (item.winAmount ?? 0) : -(item.amount ?? 0),
          ),
        ],
      ),
    );
  }

  /// 构建数据单元格
  Widget _buildDataCell(
    String text,
  ) {
    return Container(
      alignment: Alignment.centerLeft,
      width: 80.w,
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Text(
        text,
        style: TextStyle(
          fontSize: 12.sp,
          color: AppColor.textPrimary.withOpacity(0.94),
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }

  /// 构建币种单元格
  Widget _buildCurrencyCell(
    String symbol,
  ) {
    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      alignment: Alignment.centerLeft,
      width: 80.w,
      // child: CurrencyIconWidget(
      //   symbol: symbol,
      //   iconSize: 24.w,
      //   fontSize: 12.sp,
      // ),
      child: Row(
        children: [
          AutoImage(
            UtilsOther().getAmountTagIcon(symbol),
            width: 24.w,
          ),
          4.horizontalSpace,
          Text(
            symbol,
            style: TextStyle(
                color: AppColor.textPrimary.withOpacity(0.7), fontSize: 14.sp),
          )
        ],
      ),
    );
  }

  /// 构建盈亏单元格
  Widget _buildProfitLossCell(int winAmount, {int flex = 1}) {
    final isProfit = winAmount > 0;
    final color =
        isProfit ? AppColor.homePrizePoolIndicator : AppColor.gameResultEven;
    final prefix = isProfit ? '+' : '';

    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 8.w),

      alignment: Alignment.centerLeft,
      width: 80.w,
      child: Text(
        '$prefix${winAmount.formatWallet()}',
        style: TextStyle(
          fontSize: 12.sp,
          color: color,
          fontWeight: FontWeight.w900,
        ),
      ),
    );
  }

  /// 格式化区块号
  String _formatRound(int? round) {
    if (round == null) return '--';
    return round.toString();
  }

  Widget _buildHeaderCell(String title, {double width = 60}) {
    return Container(
      alignment: Alignment.centerLeft,
      width: 80.w,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w600,
          color: AppColor.textPrimary.withOpacity(0.3),
        ),
      ),
    );
  }
}
